package com.sofwin.aop;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sofwin.Constants;
import com.sofwin.annotation.Log;
import com.sofwin.pojo.SysOperatorLogWithBLOBs;
import com.sofwin.pojo.SysUser;
import com.sofwin.service.OperatorService;
import com.sofwin.util.DateUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.swing.text.StyledEditorKit;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * @packageName: com.sofwin.aop
 * @user: andyliu
 * @date: 2023/5/19 14:53
 * @email 115176513@qq.com
 * @description: TODO
 */
@Component
@Aspect
public class LogAspect {
    @Autowired
    private OperatorService service;

    @AfterReturning(pointcut = "@annotation(com.sofwin.annotation.Log)",returning = "obj")
    public void afterReturning(JoinPoint joinPoint,Object obj) throws JsonProcessingException {
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.toString());
        MethodSignature methodSignature = (MethodSignature)signature;
        Method method1 = methodSignature.getMethod();
        Log log = method1.getDeclaredAnnotation(Log.class);

//        String name = log
//        Log log = (Log) name
        String module = log.module();
        ServletRequestAttributes request1 =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = request1.getRequest();
        SysUser user =(SysUser) request.getSession().getAttribute(Constants.CURRENT_USER);
        Integer userId = user.getId();
        String operatorTime = DateUtil.getNow();
        String uri = request.getRequestURI();
        String method = request.getMethod();
        Map<String, String[]> map = request.getParameterMap();
        ObjectMapper objectMapper = new ObjectMapper();
        String params = objectMapper.writeValueAsString(map);
        String result = objectMapper.writeValueAsString(obj);
        // 将以上数据插入到数据库里
        SysOperatorLogWithBLOBs logInfo = new SysOperatorLogWithBLOBs();
        logInfo.setParams(params);
        logInfo.setUrl(uri);
        logInfo.setResult(result);
        logInfo.setFlag(0);
        logInfo.setMethod(method);
        logInfo.setModule(module);
        logInfo.setOperatorTime(operatorTime);
        logInfo.setUserId(userId);
        logInfo.setIp(request.getRemoteAddr());
        service.saveOperator(logInfo);
    }
    @AfterThrowing(pointcut = "@annotation(com.sofwin.annotation.Log)",throwing = "ex")
    public void afterThrowing(JoinPoint joinPoint,Throwable ex) throws JsonProcessingException {
        Signature signature = joinPoint.getSignature();
        System.out.println(signature.toString());
        MethodSignature methodSignature = (MethodSignature)signature;
        Method method1 = methodSignature.getMethod();
        Log log = method1.getDeclaredAnnotation(Log.class);

//        String name = log
//        Log log = (Log) name
        String module = log.module();
        ServletRequestAttributes request1 =(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = request1.getRequest();
        SysUser user =(SysUser) request.getSession().getAttribute(Constants.CURRENT_USER);
        Integer userId = user.getId();
        String operatorTime = DateUtil.getNow();
        String uri = request.getRequestURI();
        String method = request.getMethod();
        Map<String, String[]> map = request.getParameterMap();
        ObjectMapper objectMapper = new ObjectMapper();
        String params = objectMapper.writeValueAsString(map);
        // 将以上数据插入到数据库里
        SysOperatorLogWithBLOBs logInfo = new SysOperatorLogWithBLOBs();
        logInfo.setParams(params);
        logInfo.setUrl(uri);
        logInfo.setResult(objectMapper.writeValueAsString(ex));
        logInfo.setFlag(1);
        logInfo.setMethod(method);
        logInfo.setModule(module);
        logInfo.setOperatorTime(operatorTime);
        logInfo.setUserId(userId);
        logInfo.setIp(request.getRemoteAddr());
        service.saveOperator(logInfo);
    }
}
